<?php

include_once F_SOURCE . 'pic/pic.model.class.php';

/**
 * Picture 投放管理
 * 
 * @author Dean
 *
 */
class picCtrl extends Ctrl{
	
	public function show(){
		$view = new View();
	
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
	
		$params = $this->getShowParams();
	
		$picModel = new picModel();
		$number = $picModel->countAll($params);
		$picList = $picModel->findAll(($pageIndex-1)*PageNum, PageNum, $params);
	
		$page = pageCount($number, $pageIndex, '?do=pic.show');
	
		$data = array(
				'op_select' => 'pic',
				'child_select' => 'show',
				'picList' => $picList,
				'page' => $page,
				'params'=>$params
		);
	
		$view->display($data, 'pic-list.html');
	}
	
	public function view(){
		$view = new View();
		
		$additionList = $this->getAdditionList();
		
		$data = array(
				'op_select' => 'pic',
				'child_select' => 'info',
				'op' => 'add',
				'classList' => $additionList['class'],
				'channelList' => $additionList['channel'],
				'brandList' => $additionList['brand'],
				'tagList' => $additionList['tag'],
				'sizeCount' => 10,
		);
		
		$view->display($data, 'pic-insert.html');
	}
	
	public function info(){
		$id = $_GET['id'];

		$view = new View();
	
		$picModel = new picModel();
		$picInfo = $picModel->findOne($id);
	
		$additionList = $this->getAdditionList();
		
		foreach ($additionList as $type => $typeList){
			$selectArr = '';
			if($type=='class' && !empty($picInfo->class)){
					$selectArr = explode(',', $picInfo->class);
			}elseif ($type=='channel' && !empty($picInfo->channel)){
					$selectArr = explode(',', $picInfo->channel);
			}elseif ($type=='brand' && !empty($picInfo->brand)){
					$selectArr = explode(',', $picInfo->brand);
			}elseif ($type=='tag' && !empty($picInfo->tag)){
				$selectArr = explode(',', $picInfo->tag);
			}
			if($selectArr==='')continue;
			foreach ($typeList as $typekey => $typevalue){
				foreach ($selectArr as $selectValue){
					if($typevalue['name']==$selectValue) $additionList[$type][$typekey]['select'] = 'checked="checked"';
				}
			}
		}
		
		$data = array(
				'op_select' => 'pic',
				'child_select' => 'info',
				'op' => 'update',
				'obj' => $picInfo,
				'classList' => $additionList['class'],
				'channelList' => $additionList['channel'],
				'brandList' => $additionList['brand'],
				'tagList' => $additionList['tag'],
				'sizeCount' => 10,
		);
	
		$view->display($data, 'pic-insert.html');
	}
	
	/**
	 * 新增Pic投放操作
	 */
	public function add(){
	
		$name =  getPostStringParam('name');
		$order =  getPostStringParam('order');
		$text =  getPostStringParam('text');
		$text_color = getPostStringParam('text_color');
		$text_size = getPostStringParam('text_size');
		$content =  getPostStringParam('content');
		$content_color =  getPostStringParam('content_color');
		$content_size =  getPostStringParam('content_size');
		$bg_url =  getPostStringParam('bg_url');
		$out_url =  getPostStringParam('out_url');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$pic_url = getPostStringParam('pic_url');
		$weight = getPostIntParam('weight');
		$num = getPostIntParam('num');
		$channel = $_POST['channel'];
		$class = $_POST['class'];
		$brand = $_POST['brand'];
		$tag = $_POST['tag'];
		$TBD = getPostStringParam('TBD');
		$tpl = getPostIntParam('tpl');
		
		
		$time = getNowTime();
		$createUser = $_SESSION['ads']['user']->id;
	
		$id = $this->getNextID();	//get next id from DB
		
		$data = array(
				'id' => $id,
				'name' => $name,
				'order' => $order,
				'text' => $text,
				'text_color' => $text_color,
				'text_size' => $text_size,
				'content' => $content,
				'content_color' => $content_color,
				'content_size' => $content_size,
				'bg_url' => $bg_url,
				'out_url' => $out_url,
				'start' => $start,
				'end' => $end,
				'weight' => $weight,
				'pic_url' => $pic_url,
				'createTime' => $time,
				'modifyTime' => $time,
				'createUser' => $createUser,
				'num' => $num,
				'channel' => implode(',', $channel),
				'class' => implode(',', $class),
				'brand' => implode(',', $brand),
				'tag' => implode(',', $tag),
				'TBD' => $TBD,
				'tpl' => $tpl
		);
	
		$picModel = new picModel();
		$insertID = $picModel->add($data);
			
		if($insertID >= 0){
			go('index.php?do=pic.show&msg=success&id='.$insertID);
		}else{
			go('index.php?do=pic.view&msg=error');
		}
	}
	
	public function modify(){
	
		$name =  getPostStringParam('name');
		$order =  getPostStringParam('order');
		$id = getPostStringParam('id');
		$text =  getPostStringParam('text');
		$text_color = getPostStringParam('text_color');
		$text_size = getPostStringParam('text_size');
		$content =  getPostStringParam('content');
		$content_color =  getPostStringParam('content_color');
		$content_size =  getPostStringParam('content_size');
		$bg_url =  getPostStringParam('bg_url');
		$out_url =  getPostStringParam('out_url');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$pic_url = getPostStringParam('pic_url');
		$weight = getPostIntParam('weight');
		$num = getPostIntParam('num');
		$channel = $_POST['channel'];
		$class = $_POST['class'];
		$brand = $_POST['brand'];
		$tag = $_POST['tag'];
		$TBD = getPostStringParam('TBD');
		$tpl = getPostIntParam('tpl');
		
		$time = getNowTime();
	
		if(is_array($channel)){
			$channelStr = implode(',', $channel);
		}
		if(is_array($channel)){
			$classlStr = implode(',', $class);
		}
		if(is_array($channel)){
			$brandStr = implode(',', $brand);
		}
		if(is_array($channel)){
			$tagStr = implode(',', $tag);
		}
		
		$data = array(
				'name' => $name,
				'order' => $order,
				'text' => $text,
				'text_color' => $text_color,
				'text_size' => $text_size,
				'content' => $content,
				'content_color' => $content_color,
				'content_size' => $content_size,
				'bg_url' => $bg_url,
				'out_url' => $out_url,
				'start' => $start,
				'end' => $end,
				'weight' => $weight,
				'pic_url' => $pic_url,
				'num' => $num,
				'channel' => $channelStr,
				'class' => $classlStr,
				'brand' => $brandStr,
				'tag' => $tagStr,
				'modifyTime' => $time,
				'TBD' => $TBD,
				'tpl' => $tpl
		);
	
		$where = array(
				'id' => $id
		);
	
		$picModel = new picModel();
		$number = $picModel->modify($data, $where);
			
		if($number >= 0){
			go('index.php?do=pic.show&msg=success&id='.$id);
		}else{
			go('index.php?do=pic.info&msg=error&id='.$id);
		}
	}
	
	public function delete(){
	
		$id = $_GET['id'];
	
		$picModel = new picModel(); 
		$picModel->delete(array('id'=>$id));
	
		go('index.php?do=pic.show&msg=success');
	}
	
	public function showimg(){
		$id = getPostStringParam('id');
	
		$picModel = new picModel(); 
		$Info = $picModel->findOne($id);
	
		echo $Info->pic_url;
	}
	
	public function getPicAds(){
		
		global $memcache;
		
		$callback = getGetStringParam('callback');
		
		$key = 'get_pic_ads_'.date('Ymd');
		$memKey = md5($key);
		
		//$content = $memcache->get($memKey);
		if(!isset($content) || empty($content)){
			$picModel = new picModel();
			$picList = $picModel->findAllOrderByWeight();
			
			$returnArr = array();
			
			if(is_array($picList)){
				foreach ($picList as $key => $value){
					$class = $value->class;
					$channel = $value->channel;
					$brand = $value->brand;
					$tag = $value->tag;
					
					$picKey = array();
					if(isset($class) && !empty($class))$picKey[] = 'class:'.$class;
					if(isset($channel) && !empty($channel))$picKey[] = 'channel:'.$channel;
					if(isset($brand) && !empty($brand))$picKey[] = 'brand:'.$brand;
					if(isset($tag) && !empty($tag))$picKey[] = 'tag:'.$tag;
					
					if(count($picKey)<=0)continue;
					
					if(!isset($returnArr[implode(';', $picKey)])){
						$tmp = 'id='.$value->id.'&s=hd&tpl=' . $value->tpl;
						if($value->tpl==3){
							$tmp .= '&url=' . urlencode($value->out_url);
						}
						$tmp .= '&';
						$returnArr[implode(';', $picKey)] = $tmp;
					}
				}
				$json =  json_encode($returnArr);
				
				if($_GET['debug_mem']){
					echo 'get content by DB <br />';
				}
				
				$memcache->set($memKey, $json, MEMCACHE_COMPRESSED, 1800);
				echo checkCallBack($callback, $json);
			}
			exit;
		}
		
		echo checkCallBack($callback, $content);
		exit;
	}
	
	public function getPicAdsHTML(){
		
		global $memcache;
		
		$id = getGetStringParam('id');
		$key = 'get_pic_ads_html_'.$id.'_'.date('Ymd');
		$memKey = md5($key);
		
		$content = $memcache->get($memKey);
		if(!isset($content) || empty($content)){
			$picModel = new picModel();
			$picInfo = $picModel->findOne($id);
			if(!isset($picInfo->id)){
				die('wrong ID!');
			}
	
			$action_host = "http://".$_SERVER['HTTP_HOST']."/upload_file/image/";
			
			$htmlContent = file_get_contents(F_ROOT.'template/pic_show_mockup_' . $picInfo->tpl . '.html');
			
			$htmlContent = str_replace("ICON_HOLDER", $action_host.$picInfo->pic_url, $htmlContent);
			$htmlContent = str_replace("BG_HOLDER", $action_host.$picInfo->bg_url, $htmlContent);
			$htmlContent = str_replace("TITLE_HOLDER", $picInfo->text, $htmlContent);
			$htmlContent = str_replace("TITLE_COLOR_HOLDER", $picInfo->text_color, $htmlContent);
			$htmlContent = str_replace("TITLE_SIZE_HOLDER", $picInfo->text_size.'px', $htmlContent);
			$htmlContent = str_replace("CONTENT_HOLDER", $picInfo->content, $htmlContent);
			$htmlContent = str_replace("CONTENT_COLOR_HOLDER", $picInfo->content_color, $htmlContent);
			$htmlContent = str_replace("CONTENT_SIZE_HOLDER", $picInfo->content_size.'px', $htmlContent);
			
			if(stripos($picInfo->out_url, "http://")===false){
				$picInfo->out_url = "http://".$picInfo->out_url;
			}
			$htmlContent = str_replace("OUT_URL_HOLDER", $picInfo->out_url, $htmlContent);
			
			if($_GET['debug_mem']){
				echo 'get content by DB <br />';
			}
			
			$memcache->set($memKey, $htmlContent, MEMCACHE_COMPRESSED, 1800);
			echo $htmlContent;
			exit;
		}
		
		echo $content;
		exit;
	}
	
	public function previewInsert(){
		
		$pic_url = urldecode(getGetStringParam('pic_url'));
		$bg_url = urldecode(getGetStringParam('bg_url'));
		$out_url = urldecode(getGetStringParam('out_url'));
		$text = urldecode(getGetStringParam('text'));
		$text_color = urldecode(getGetStringParam('text_color'));
		$text_size = urldecode(getGetStringParam('text_size'));
		$content = urldecode(getGetStringParam('content'));
		$content_color = urldecode(getGetStringParam('content_color'));
		$content_size = urldecode(getGetStringParam('content_size'));
		
		$tpl = urldecode(getGetIntParam('tpl'));
		
		$action_host = "http://".$_SERVER['HTTP_HOST']."/upload_file/image/";
		
		$htmlContent = file_get_contents(F_ROOT.'template/pic_show_mockup_'.$tpl.'.html');
		
		$htmlContent = str_replace("ICON_HOLDER", $action_host.$pic_url, $htmlContent);
		$htmlContent = str_replace("BG_HOLDER", $action_host.$bg_url, $htmlContent);
		$htmlContent = str_replace("TITLE_HOLDER", $text, $htmlContent);
		$htmlContent = str_replace("TITLE_COLOR_HOLDER", $text_color, $htmlContent);
		$htmlContent = str_replace("TITLE_SIZE_HOLDER", $text_size.'px', $htmlContent);
		$htmlContent = str_replace("CONTENT_HOLDER", $content, $htmlContent);
		$htmlContent = str_replace("CONTENT_COLOR_HOLDER", $content_color, $htmlContent);
		$htmlContent = str_replace("CONTENT_SIZE_HOLDER", $content_size.'px', $htmlContent);
		
		if(stripos($picInfo->out_url, "http://")===false){
			$picInfo->out_url = "http://".$picInfo->out_url;
		}
		$htmlContent = str_replace("OUT_URL_HOLDER", $picInfo->out_url, $htmlContent);
		
		echo $htmlContent;
		exit;
	}
	
	
	private function getShowParams(){
		$data = array();
	
		$data['v_id'] = getPostStringParam('v_id');
		$data['v_order'] = getPostStringParam('v_order');
		$data['v_activity'] = getPostStringParam('v_activity');
		$data['v_name'] = getPostStringParam('v_name');
		$data['v_ads_id'] = getPostStringParam('v_ads_id');
		$data['v_start'] = getPostStringParam('v_start');
		$data['v_end'] = getPostStringParam('v_end');
		$data['v_priority'] = getPostStringParam('v_priority');
		$data['v_target_window'] = getPostStringParam('v_target_window');
		$data['v_size'] = getPostStringParam('v_size');
	
		return $data;
	}
	
	private function getNextID(){
		$picModel = new picModel();
		$picInfo = $picModel->getMaxID();
		
		$nextID = 'HD100001';
		
		if(isset($picInfo->id)){
			$maxnum = intval(str_replace('HD', '', $picInfo->id));
			$nextID = 'HD'.($maxnum+1);
		}
		return $nextID;
	}
	
	private function getAdditionList(){
		
		$picModel = new picModel();
		$list = $picModel->getAddition();
		
		$arr = array();
		if(is_array($list)){
			foreach ($list as $value) {
				$arr[$value->type][]['name'] = $value->name;
			}
		}
		
		return $arr;
	}
	
	public function searchPics(){
		
		$channel = $_POST['channel'];
		$class = $_POST['class'];
		$brand = $_POST['brand'];
		$tag = $_POST['tag'];
	
		$solrUrl = include F_CONF.'solr.config.php';
	
		$channel_q = $this->post_q($channel, 'channel');
		if($channel_q!=='')$qArr[] = $channel_q;
		$class_q = $this->post_q($class, 'class');
		if($class_q!=='')$qArr[] = $class_q;
		$brand_q = $this->post_q($brand, 'brand');
		if($brand_q!=='')$qArr[] = $brand_q;
		$tag_q = $this->post_q($tag, 'tag');
		if($tag_q!=='')$qArr[] = $tag_q;
		
		$q = implode(' AND ', $qArr);
		
		// find pic number by solr
		$picArr = searchBySolr($solrUrl['pic'], $q);
	
		$picnum = count($picArr);
		echo $picnum;
	}
	
	private function post_q($str, $field){
		if(empty($str))return '';
		
		$channelArr = explode(',', $str);
		foreach ($channelArr as $value){
			$m[]= $value;
		}
		$e = "$field:(".implode(' ', $m).")";
		return $e;
	}
}